package com.shallwead.bna.Async;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import com.shallwead.bna.BnA;
import com.shallwead.bna.dialog.PushPopupDialog;
import com.shallwead.bna.object.AdGcmMsg;
import com.shallwead.bna.util.Logger;

public class PushDialogAsync extends AsyncTask<String, String, Bitmap> {

	Context mContext;
	Bitmap bm;
	Notification notify;
	NotificationManager nm;

	public PushDialogAsync(Context mContext) {
		this.mContext = mContext;
	}

	@Override
	protected Bitmap doInBackground(String... params) {

		Bitmap bm = null;
		try {
			bm = getBitmap(BnA.gcmMsg.getImageUrl());
			BnA.gcmImg = bm;
		} catch (Exception e) {
			Logger.e(e.toString());
		}

		return bm;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		super.onPostExecute(result);
		Logger.e("complete p_dialog bm");
		if (result != null && BnA.gcmMsg != null) {
			Logger.e("send dialog");
			send_Dialog();
		}
	}

	void send_Dialog() {
		mContext.startActivity(new Intent(mContext, PushPopupDialog.class)
				.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
	}

	public Bitmap getBitmap(String url) {
		Bitmap bm = null;
		try {
			URL aURL = new URL(url);
			URLConnection conn = aURL.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			bm = BitmapFactory.decodeStream(bis);
			bis.close();
			is.close();
		} catch (IOException e) {

		}
		return bm;
	}
}